package com.sgb.kjwl.viewmodel.machine;

import androidx.databinding.ObservableField;


import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.MachineDetailEntity;
import com.sgb.kjwl.repository.MachineRepertory;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.self.MachinePublishEvent;
import com.sgb.kjwl.view.ui.activity.leaseAsk.MachineOutActivity;
import com.sgb.kjwl.view.ui.activity.pick.PickInquireByLetterActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.im.immodel.IMUtils;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 机械详情
 */
public class MachineDetailModel extends BaseViewModel {

    private final MachineOutActivity machineOutActivity;
    private final MachineRepertory machineRepertory;

    public ObservableField<MachineDetailEntity> detail = new ObservableField<>(new MachineDetailEntity());

    public MachineDetailModel(MachineOutActivity machineOutActivity, MachineRepertory machineRepertory) {
        this.machineOutActivity = machineOutActivity;
        this.machineRepertory = machineRepertory;
    }

    public void getDetailModel(String id) {
        DialogHelper.showProgressDialog(machineOutActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getMachineAPI().getDetail(id).enqueue(new Callback<BaseEntity<MachineDetailEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<MachineDetailEntity>> call, Response<BaseEntity<MachineDetailEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    machineOutActivity.showDetail(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<MachineDetailEntity>> call, Throwable t) {
                MToast.showToast(machineOutActivity, t.getMessage());
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getDeleteModel(String id, String status) {
        Map map = new HashMap<>();
        map.put("id", id);
        map.put("status", status);
        DialogHelper.showProgressDialog(machineOutActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getMachineAPI().delMachineOutList(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    MToast.showToast(machineOutActivity, "删除成功");
                    EventBus.getDefault().post(MachinePublishEvent.make().setMessageTag(EventConstant.EVENT_PUBLISH_MACHINE_REFUSH));
                    machineOutActivity.finish();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                MToast.showToast(machineOutActivity, t.getMessage());
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getOnlineModel(String id, String status) {
        Map map = new HashMap<>();
        map.put("id", id);
        map.put("status", status);
        DialogHelper.showProgressDialog(machineOutActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getMachineAPI().machineOutOnlineOrOff(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (status.equals("1")) {
                        MToast.showToast(machineOutActivity, "操作成功");
                    } else if (status.equals("2")) {
                        MToast.showToast(machineOutActivity, "操作成功");
                    }
                    EventBus.getDefault().post(MachinePublishEvent.make().setMessageTag(EventConstant.EVENT_PUBLISH_MACHINE_REFUSH));
//                    machineOutActivity.finish();
                    machineOutActivity.refreshData();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                MToast.showToast(machineOutActivity, t.getMessage());
                DialogHelper.dismissProgressDialog();
            }
        });
    }

//    //交流
//    public void findCustomerServiceImAccount(String customerServiceContactsId) {
//        DialogHelper.showProgressDialog(machineOutActivity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
//        Map map = new HashMap();
//        map.put("customerServiceContactsId", customerServiceContactsId);
//        APIManager.getInstance().getPickAPI().getfindCustomerServiceImAccount(map).enqueue(new Callback<BaseEntity>() {
//            @Override
//            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
//                DialogHelper.dismissProgressDialog();
//                if (response.body() == null) return;
//                if (response.body().getState().equals(APIManager.RESPONSE_FAIL)) {
////                    MToast.showToast(activity, response.body().getMsg());
//                    machineOutActivity.checkAllowDialog(response.body().getMsg());
//                } else {
//                    String dataIMid = response.body().getData().toString();// 接口返回的是String类型
//                    NimUIKit.startP2PSession(machineOutActivity, dataIMid);
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity> call, Throwable t) {
//                DialogHelper.dismissProgressDialog();
//            }
//        });
//
//    }

    //判断是否能交流
    public void judgeCompanyOrSelf(String imAccount, String createCompanyNo,String createUserNo) {
        DialogHelper.showProgressDialog(machineOutActivity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getMachineAPI().judgeCompanyOrSelf(createCompanyNo, createUserNo).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                try {
                    JSONObject jsonObject = new JSONObject(response.body().getData().toString());
                    int data = jsonObject.getInt("data");
                    String msg = jsonObject.getString("msg");
                    if (data==0) {
//                        findCustomerServiceImAccount(id);
//                        NimUIKit.startP2PSession(machineOutActivity, imAccount);
                        IMUtils.getInstance().buildSingleTalking(imAccount);
                    } else {
                        machineOutActivity.checkAllowDialog(msg);
                    }
                } catch (Exception e) {
                    machineOutActivity.checkAllowDialog("不可对自己或自己公司交流");
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 检查当前用户是否允许租赁函询
     *
     * @param id
     */
    public void checkAllowZlQuestion(String id, String type, String createUserNo, String createCompanyNo, String markType) {
        DialogHelper.showProgressDialog(machineOutActivity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().checkAllowZlQuestion(id, markType).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                try {
                    JSONObject jsonObject = new JSONObject(response.body().getData().toString());
                    boolean b = jsonObject.getBoolean("flag");
                    String msg = jsonObject.getString("msg");
                    if (b) {
                        PickInquireByLetterActivity.start(machineOutActivity, id, type, createCompanyNo, createUserNo, "2");//2代表机械函询
                    } else {
                        machineOutActivity.checkAllowDialog(msg);
                    }
                } catch (Exception e) {
                    machineOutActivity.checkAllowDialog("不可函询");
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    //是否被关注
    public void judgeLease(String id) {
        DialogHelper.showProgressDialog(machineOutActivity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().judgeLease(id).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                try {
                    if(response.body()!=null){
                        JSONObject jsonObject = new JSONObject(response.body().getData().toString());
                        int data = jsonObject.getInt("data");
                        machineOutActivity.setTvGz(data == 1);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 关注或取消关注
     *
     * @param map
     */
    public void leaseFocus(Map map) {
        DialogHelper.showProgressDialog(machineOutActivity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().leaseFocus(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                try {
                    JSONObject jsonObject = new JSONObject(response.body().getData().toString());
                    int data = jsonObject.getInt("data");
                    if (data == 1) {
                        machineOutActivity.setLeaseFocus();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

}
